package com.pams.service;


import cn.hutool.core.date.DateTime;
import com.pams.context.BaseContext;
import com.pams.handler.BaseException;
import com.pams.mapper.JournalMapper;
import com.pams.pojo.entity.Journal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JournalService {
    @Autowired
    private JournalMapper journalMapper;
    public Integer add(Journal journal) {
        String currentUsername = BaseContext.getCurrentUsername();
        if(journal.getTitle() == null || journal.getTitle() == ""){
            throw new BaseException("标题不能为空");
        }
        Journal byId = journalMapper.getById(journal.getId());
        if(byId == null ){
            journal.setUsername(currentUsername);
        journalMapper.insert(journal);
          return  journal.getId();
        }
        journal.setUsername(currentUsername);
       journalMapper.update(journal);
        return  journal.getId();
    }

    public List<Journal> getlist() {
        String currentUsername = BaseContext.getCurrentUsername();
       List<Journal> list = journalMapper.getlist(currentUsername);
       return list;
    }

    public Journal getSelectedJournal(int id) {
        return journalMapper.getById(id);
    }

    public void delete(int id) {
        journalMapper.delete(id);
    }

    public List<Journal> search(String title) {
        String currentUsername = BaseContext.getCurrentUsername();
        List<Journal> list = journalMapper.search(currentUsername,title);
      return list;
    }

}
